package com.mxpio.erp.plm.controller;

import com.mxpio.erp.common.plm.entity.ProjectProblem;
import com.mxpio.erp.common.plm.entity.ProjectProblemStatusHistory;
import com.mxpio.erp.common.plm.service.ProjectProblemStatusHistoryService;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.query.Criteria;
import com.mxpioframework.jpa.query.Operator;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.util.List;

@Tag(name = "ProjectProblemStatusHistoryController", description = "项目任务问题的状态记录表")
@RestController
@RequestMapping("/erp/plm/pro/problem/status/history/")
public class ProjectProblemStatusHistoryController {
    @Autowired
    private ProjectProblemStatusHistoryService projectProblemStatusHistoryService;


    @GetMapping("list")
    @Operation(summary = "状态历史记录列表", description = "状态历史记录列表（无分页）", method = "GET")
    public Result<List<ProjectProblemStatusHistory>> list(@Parameter(description="查询参数") Criteria criteria) throws UnsupportedEncodingException {
        List<ProjectProblemStatusHistory> list = projectProblemStatusHistoryService.list(ProjectProblemStatusHistory.class, criteria);
        return Result.OK(list);
    }

    @GetMapping("page")
    @Operation(summary = "状态历史记录列表", description = "状态历史记录列表（分页）", method = "GET")
    public Result<Page<ProjectProblemStatusHistory>> page(@Parameter(description="查询参数") Criteria criteria, @Parameter(description="每页条数")Integer pageSize, @Parameter(description="页号")Integer pageNo) throws UnsupportedEncodingException {
        Pageable page = PageRequest.of(pageNo-1, pageSize);
        Page<ProjectProblemStatusHistory> pageResult = projectProblemStatusHistoryService.listPage(ProjectProblemStatusHistory.class, page, criteria);
        return Result.OK(pageResult);
    }

    @RequestMapping("add")
    @Operation(summary = "保存状态历史记录", description = "保存状态历史记录")
    public Result<ProjectProblemStatusHistory> add(@RequestBody ProjectProblemStatusHistory projectProblemStatusHistory) {
        projectProblemStatusHistoryService.save(projectProblemStatusHistory);
        return Result.OK(projectProblemStatusHistory);
    }

    @GetMapping("list/{questionCode}")
    @Operation(summary = "根据问题编码获取状态记录", description = "根据问题编码获取状态记录", method = "GET")
    public Result<List<ProjectProblemStatusHistory>> getById(@PathVariable(name = "questionCode", required = true) String questionCode) {
        Criteria criteria = Criteria.create().addCriterion("questionCode", Operator.EQ, questionCode);
        List<ProjectProblemStatusHistory> list = projectProblemStatusHistoryService.list(ProjectProblemStatusHistory.class, criteria);
        return Result.OK(list);
    }
}
